package com.symone.webcms.service.impl;

import com.symone.webcms.model.SiteNav;
import com.symone.webcms.mapper.SiteNavMapper;
import com.symone.webcms.service.SiteNavService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 *　　
 *   @description : SiteNav 服务实现类
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-23
 */
@Service
public class SiteNavServiceImpl extends ServiceImpl<SiteNavMapper, SiteNav> implements SiteNavService {

    @Override
    public Page<SiteNav> findPage(Page<SiteNav> page, Wrapper<SiteNav> siteNav) {
        List<SiteNav> result = baseMapper.selectPage(page,siteNav);
        page.setRecords(result);
        return page;
    }

    @Override
    public List<SiteNav> findAll() {
        Wrapper<SiteNav> ew = new EntityWrapper<SiteNav>();
        ew.where("deleted=0");
        return baseMapper.selectList(ew);
    }

    @Override
    public int delete(long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public boolean deleteFlagById(long id) {
        return baseMapper.deleteFlagById(id);
    }

    @Override
    public int clear() {
        return baseMapper.delete(new EntityWrapper<SiteNav>().eq("deleted" ,1));
    }

    @Override
    public List<SiteNav> getNavTree(String lang) {
        List<SiteNav> navs = baseMapper.selectList(
                new EntityWrapper<SiteNav>()
                .eq("deleted",0)
                .and()
                .eq("lang",lang)
                .orderBy("sorting,id")
        );
        List<SiteNav> result = new ArrayList<SiteNav>();
        for (SiteNav nav : navs){
            if(nav.getPid() == 0){
                List<SiteNav> children = new ArrayList<>();
                for (SiteNav sub : navs){
                    if(sub.getPid() > 0 && sub.getPid().equals(nav.getId())){
                        children.add(sub);
                    }
                }
                nav.setChildren(children);
                result.add(nav);
            }
        }
        return result;
    }

}
